﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagChina.Basic.CCT.PLC;
using HomagGroup.Base.Communication.Items;
using System;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Threading;
using System.Threading.Tasks;

namespace Proxy
{
    public class InfeedHandle
    {
        private InsertInfoHandle Handle_Insert;
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;
        private CommonHandle Handle_Common;
        private InfeedTransportHandle Handle_InfeedTransport;
        private PlcHandle Handle_Plc;

        public InfeedHandle()
        {
            Handle_Insert = new InsertInfoHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Common = new CommonHandle();
            Handle_InfeedTransport = new InfeedTransportHandle();
            Handle_Plc = new PlcHandle();
            Task.Factory.StartNew(() => Position_Infeed());
            Task.Factory.StartNew(() => Handle_InfeedTransport.Position_Transport("CT020"));
            Task.Factory.StartNew(() => Handle_InfeedTransport.Position_Transport("BC025"));
        }

        /// <summary>
        /// 
        /// </summary>
        private void Position_Infeed()
        {
            do
            {
            Start:
                Thread.Sleep(1000);
                try
                {
                    if (Globle.isInfeedAreaActive)
                    {
                        var orderState = (int)(Handle_Plc.Read(Wholesale.Item_AIF_CT015_OrderState, "Int"));
                        if (!orderState.Equals((int)EnumOfOrderState.Plc请求数据)
                            && !orderState.Equals((int)EnumOfOrderState.Plc完成任务))
                        {
                            goto Start;
                        }

                        var OnLineInfo = Handle_Search.GetTransOnLineDetailInfo("", "CT015");
                        if (OnLineInfo == null)
                        {
                            goto Start;
                        }
                        var Reference = Handle_Search.GetTransOnLineDetailInfo("", "CT015").transOnlineRecords.Reference;
                        SerilogHandle.Logger_CT015.Information("Reference :" + OnLineInfo.boardInfo.Reference);
                        SerilogHandle.Logger_CT015.Information("orderState :" + orderState);
                        switch (orderState)
                        {
                            case (int)EnumOfOrderState.Plc完成任务:
                            Write2Plc:
                                ResultCode WriteFlag = Handle_Plc.Write(Wholesale.Item_AIF_CT015_OrderState, (int)EnumOfOrderState.CCT完成任务);
                                if (WriteFlag != ResultCode.Success)
                                {
                                    goto Write2Plc;
                                }
                                Handle_Modify.ModifyTransState(Reference);
                                break;
                            case (int)EnumOfOrderState.Plc请求数据:
                                Handle_Plc.Write(Wholesale.Item_AIF_CT015_Reference, Reference);
                                Handle_Plc.Write(Wholesale.Item_AIF_CT015_Direction, (int)EnumOfDirection.Forward);
                                Handle_Plc.Write(Wholesale.Item_AIF_CT015_OrderState, (int)EnumOfOrderState.CCT下发任务);
                                break;
                            default:
                                break;
                        }
                        goto Start;
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_CT015.Error(ex.Message);
                    goto Start;
                }
            } while (true);
        }
    }
}